<?php
namespace app\applet\controller\wxapp;

use app\applet\controller\Controller;
use app\applet\model\Apply as ApplyModel;
use app\applet\model\UserDetail as UserDetailModel;
use app\applet\model\Setting;
use think\facade\View;

/**
 * 用户注册申请
 */
class Register extends Controller
{
    /**
     * 构造方法
     */
    public function initialize()
    {
        parent::initialize();
        View::assign(['ability' => Setting::getItem('ability',0)]);
    }
    /**
     * 注册小程序
     */
    public function wxapp()
    {	
    	$model = ApplyModel::getApply([
    		'apply_mode' => 10,
    		'applet_id' => $this->applet_id,
    		'user_id' => $this->user['user']['user_id']
    	]);
		if(!$this->request->isAjax()) {
			if(!$model){
				$model['details'] = UserDetailModel::getUserDetail($this->user['user']['user_id']);
			}
			return View::fetch('wxapp', compact('model'));
        }
        //提交动作
		$data = $this->postData('data');
		if (!isset($data['details']['license_copy']) || empty($data['details']['license_copy'])) {
            $this->error = '请上传营业执照';
            return false;
        }
        if (!isset($data['details']['id_card_copy']) || empty($data['details']['id_card_copy'])) {
            $this->error = '请上传身份证（正面）';
            return false;
        }
        if (!isset($data['details']['id_card_national']) || empty($data['details']['id_card_national'])) {
            $this->error = '请上传身份证（反面）';
            return false;
        }
		if(!$model){
			$model = new ApplyModel;
			$data['business_code'] = order_no();//业务申请编号
			$data['user_id'] = $this->user['user']['user_id'];
			$data['applet_id'] = $this->applet_id;
		}
        if ($model->action($data)) {
			return $this->renderSuccess('提交成功，等待审核', url('wxapp/index'));
        }
        $error = $model->getError() ?: '提交失败';
        return $this->renderError($error);      
    }

    /**
     * 注册支付商户号
     */
    public function wxpay()
    {	
    	$model = ApplyModel::getApply([
    		'apply_mode' => 30,
    		'applet_id' => $this->applet_id,
    		'user_id' => $this->user['user']['user_id']
    	]);
        if(!$this->request->isAjax()) {
			if(!$model){
				$model['details'] = UserDetailModel::getUserDetail($this->user['user']['user_id']);
			}
			return View::fetch('wxpay', compact('model'));
        }
        //提交动作
		$data = $this->postData('data');
		if (!isset($data['details']['license_copy']) || empty($data['details']['license_copy'])) {
            $this->error = '请上传营业执照';
            return false;
        }
        if (!isset($data['details']['id_card_copy']) || empty($data['details']['id_card_copy'])) {
            $this->error = '请上传身份证（正面）';
            return false;
        }
        if (!isset($data['details']['id_card_national']) || empty($data['details']['id_card_national'])) {
            $this->error = '请上传身份证（反面）';
            return false;
        }
        if (!isset($data['details']['qualifications']) || empty($data['details']['qualifications'])) {
            $this->error = '请上传特殊行业资质证书';
            return false;
        }
        if (!isset($data['details']['store_entrance_pic']) || empty($data['details']['store_entrance_pic'])) {
            $this->error = '请上传门头照片';
            return false;
        }
        if (!isset($data['details']['indoor_pic']) || empty($data['details']['indoor_pic'])) {
            $this->error = '请上传店内照片';
            return false;
        }
		$data['app_id'] = $this->user['applet']['app_id'];
		if(!$model){
			$model = new ApplyModel;
			$data['business_code'] = order_no();//业务申请编号
			$data['user_id'] = $this->user['user']['user_id'];
			$data['applet_id'] = $this->applet_id;
		}
		if ($model->action($data)) {
			return $this->renderSuccess('提交成功，等待审核', url('payment/wxpay'));
        }
        $error = $model->getError() ?: '提交失败';
        return $this->renderError($error);   
    }

}
